package StudyCode.summary.zipfile;

import java.io.ByteArrayOutputStream;
import java.util.zip.Deflater;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class CompressesDataUsingZlibCompression {
	public static void main(String[] argv) {
		byte[] data = new byte[]{34, 35, 36, 37, 37, 37, 67, 68, 69};
		int offset = 2;
		int len = 2;
		System.out.println("使用 zlib 压缩数据:");
		System.out.println(java.util.Arrays.toString(deflateData(data, offset, len)));
	}


	public static byte[] deflateData(byte[] data, int offset, int len) {
		ByteArrayOutputStream stream = new ByteArrayOutputStream(
				data.length);
		byte[] buffer = new byte[data.length];
		Deflater def = new Deflater();

		def.setInput(data, offset, len);
		def.finish();

		while (!def.finished()) {
			int d = def.deflate(buffer);
			stream.write(buffer, 0, d);
		}

		return stream.toByteArray();
	}
}
